package com.boyan.auth.controller;


import com.boyan.auth.domain.dto.LoginFormDTO;
import com.boyan.auth.domain.dto.UserFomDTO;
import com.boyan.auth.domain.query.UserQuery;
import com.boyan.auth.domain.vo.UserVO;
import com.boyan.auth.service.IUserService;
import com.boyan.pojo.Result;
import com.boyan.auth.domain.po.User;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/users")
@Tag(name = "用户管理",description = "用户管理相关接口")
@RequiredArgsConstructor
@Slf4j
//lombok RequiredArgsConstructor的构造器注解，会生成一个包含所有 final 字段的构造器
public class UserController {

    private final IUserService userService;


    @Operation(summary = "新增用户",description = "新增用户")
    @PostMapping
    public Result saveUser(@RequestBody UserFomDTO userFomDTO){
        User user = new User();
        BeanUtils.copyProperties(userFomDTO,user);
        userService.save(user);

        return Result.success();
    }

    @Operation(summary = "更新用户",description = "更新用户")
    @PutMapping("/update")
    public Result updateUser(@RequestBody UserFomDTO userFomDTO){

        User user = new User();
        BeanUtils.copyProperties(userFomDTO,user);
        userService.updateById(user);
        return Result.success();



    }

    @Operation(summary = "查询用户",description = "查询用户")
    @GetMapping("/{id}")
    public Result getUserById(@PathVariable Long id){
        return userService.getUserById(id);
    }

    @Operation(summary = "删除用户",description = "删除用户")
    @DeleteMapping("/{id}")
    public Result deleteUserById(@Schema(description = "用户ID") @PathVariable("id") Long id){
        userService.removeById(id);
        return Result.success();
    }


    @Operation(summary = "查询用户列表",description = "查询用户列表")
    @GetMapping
    public Result getUsersByIds(@RequestParam("ids") List<Long> ids) {
        List<User> users = userService.listByIds(ids);
        // 使用BeanUtils将User转换为UserVO（需要自行实现转换逻辑）
        List<UserVO> voList = users.stream()
                .map(
                        user -> {
                            UserVO userVO = new UserVO();
                            BeanUtils.copyProperties(user, userVO);
                            return userVO;
                        }
                )
                .collect(Collectors.toList());
        return Result.success(voList);
    }

    @Operation(summary = "用户余额扣减",description = "用户余额扣减")
    @PutMapping("{id}/deduction/{money}")
    public Result deduction(@PathVariable Long id, @PathVariable BigDecimal money) {
        log.info("用户余额扣减开始...");
        userService.deduction(id, money);
        return Result.success();
    }

    @Operation(summary = "复杂查询",description = "复杂查询")
    @GetMapping("/queryUsers")
    public Result query(@ParameterObject UserQuery userQuery){
        log.info("复杂查询 {}", userQuery);
        String username = userQuery.getUsername();
        Integer status = userQuery.getStatus();
        BigDecimal minBalance = userQuery.getMinBalance();
        BigDecimal maxBalance = userQuery.getMaxBalance();
        log.info("username: {}, status: {}, minBalance: {}, maxBalance: {}", username, status, minBalance, maxBalance);
        List<UserVO>userLists=userService.queryByCondition(username, status, minBalance, maxBalance);
        return Result.success(userLists);

    }

    @Operation(summary = "发送验证码",description = "发送验证码")
    @GetMapping("/sendCode/send")
    public Result sendCode(@RequestParam String phone, HttpSession session){
        log.info("发送验证码...");
        return userService.sendCode(phone,session);
    }
    @Operation(summary = "登录",description = "登录")
    @PostMapping("/login")
    public Result login(@ParameterObject LoginFormDTO loginForm, HttpSession session){
        log.info("登录...");
        return userService.login(loginForm,session);
    }





}
